import {Component, OnInit} from '@angular/core';
import {AddSysPermissionsComponent} from '../add-sys-permissions.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {SysPermissions} from '../../sys-permissions.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {SysMenuCondition} from '../../../sys-menu/sys-menu.type';

@Component({
    selector: 'app-main-add-sys-permissions',
    templateUrl: './main-add-sys-permissions.component.html',
    styleUrls: ['./main-add-sys-permissions.component.css']
})
export class MainAddSysPermissionsComponent implements OnInit {

    errRes = new ErrRes();

    domain = new SysPermissions();

    menuDomains = [];

    extend = {
        domain: {
            default: () => {
                this.domain.serAudit = 'WAIT_SUBMIT';
                this.domain.serUsing = 'ENABLE';
            },
            serAudit: () => {
                return 'serAudit';
            },
        },
    };

    func = {
        save: {
            req: (event) => {
                Services.sysPermissions.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    HttpRequestClient.router.navigate([UrlRou.sysPermissions.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    initData = () => {
        const sysMenu = new SysMenuCondition();
        Services.sysMenu.list(sysMenu, {
            page: 0,
            size: 2000,
            orders: 'serNum-asc'
        }).subscribe(item => {
            this.menuDomains = item.page.content;
        });
    };

    constructor(public addSysPermissions: AddSysPermissionsComponent) {
        this.addSysPermissions.main = this;
    }

    ngOnInit() {
        this.extend.domain.default();
        this.initData();
    }

}
